package com.google.android.material.datepicker;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.InsetDrawable;
import android.graphics.drawable.StateListDrawable;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.fragment.app.u;
import b.f.l.s;
import c.b.a.b.c0.g;
import c.b.a.b.e;
import c.b.a.b.f;
import c.b.a.b.h;
import c.b.a.b.j;
import c.b.a.b.k;
import com.google.android.material.datepicker.a;
import com.google.android.material.internal.CheckableImageButton;
import java.util.Iterator;
import java.util.LinkedHashSet;

public final class i<S> extends androidx.fragment.app.c {
    static final Object F = "CONFIRM_BUTTON_TAG";
    static final Object G = "CANCEL_BUTTON_TAG";
    static final Object H = "TOGGLE_BUTTON_TAG";
    private boolean A;
    private TextView B;
    /* access modifiers changed from: private */
    public CheckableImageButton C;
    private g D;
    /* access modifiers changed from: private */
    public Button E;
    /* access modifiers changed from: private */
    public final LinkedHashSet<j<? super S>> p = new LinkedHashSet<>();
    /* access modifiers changed from: private */
    public final LinkedHashSet<View.OnClickListener> q = new LinkedHashSet<>();
    private final LinkedHashSet<DialogInterface.OnCancelListener> r = new LinkedHashSet<>();
    private final LinkedHashSet<DialogInterface.OnDismissListener> s = new LinkedHashSet<>();
    private int t;
    /* access modifiers changed from: private */
    public d<S> u;
    private p<S> v;
    private a w;
    private h<S> x;
    private int y;
    private CharSequence z;

    class a implements View.OnClickListener {
        a() {
        }

        public void onClick(View view) {
            Iterator it = i.this.p.iterator();
            while (it.hasNext()) {
                ((j) it.next()).a(i.this.x());
            }
            i.this.d();
        }
    }

    class b implements View.OnClickListener {
        b() {
        }

        public void onClick(View view) {
            Iterator it = i.this.q.iterator();
            while (it.hasNext()) {
                ((View.OnClickListener) it.next()).onClick(view);
            }
            i.this.d();
        }
    }

    class c implements o<S> {
        c() {
        }

        public void a(S s) {
            Button button;
            boolean z;
            i.this.D();
            if (i.this.u.f()) {
                button = i.this.E;
                z = true;
            } else {
                button = i.this.E;
                z = false;
            }
            button.setEnabled(z);
        }
    }

    class d implements View.OnClickListener {
        d() {
        }

        public void onClick(View view) {
            i.this.C.toggle();
            i iVar = i.this;
            iVar.E(iVar.C);
            i.this.B();
        }
    }

    static boolean A(Context context) {
        TypedArray obtainStyledAttributes = context.obtainStyledAttributes(c.b.a.b.z.b.c(context, c.b.a.b.b.materialCalendarStyle, h.class.getCanonicalName()), new int[]{16843277});
        boolean z2 = obtainStyledAttributes.getBoolean(0, false);
        obtainStyledAttributes.recycle();
        return z2;
    }

    /* access modifiers changed from: private */
    public void B() {
        this.x = h.s(this.u, y(requireContext()), this.w);
        this.v = this.C.isChecked() ? k.d(this.u, this.w) : this.x;
        D();
        u i = getChildFragmentManager().i();
        i.p(f.mtrl_calendar_frame, this.v);
        i.j();
        this.v.a(new c());
    }

    public static long C() {
        return l.s().h;
    }

    /* access modifiers changed from: private */
    public void D() {
        String v2 = v();
        this.B.setContentDescription(String.format(getString(j.mtrl_picker_announce_current_selection), new Object[]{v2}));
        this.B.setText(v2);
    }

    /* access modifiers changed from: private */
    public void E(CheckableImageButton checkableImageButton) {
        this.C.setContentDescription(checkableImageButton.getContext().getString(this.C.isChecked() ? j.mtrl_picker_toggle_to_calendar_input_mode : j.mtrl_picker_toggle_to_text_input_mode));
    }

    private static Drawable t(Context context) {
        StateListDrawable stateListDrawable = new StateListDrawable();
        stateListDrawable.addState(new int[]{16842912}, b.a.k.a.a.d(context, e.ic_calendar_black_24dp));
        stateListDrawable.addState(new int[0], b.a.k.a.a.d(context, e.ic_edit_black_24dp));
        return stateListDrawable;
    }

    private static int u(Context context) {
        Resources resources = context.getResources();
        int dimensionPixelSize = resources.getDimensionPixelSize(c.b.a.b.d.mtrl_calendar_navigation_height) + resources.getDimensionPixelOffset(c.b.a.b.d.mtrl_calendar_navigation_top_padding) + resources.getDimensionPixelOffset(c.b.a.b.d.mtrl_calendar_navigation_bottom_padding);
        int dimensionPixelSize2 = resources.getDimensionPixelSize(c.b.a.b.d.mtrl_calendar_days_of_week_height);
        return dimensionPixelSize + dimensionPixelSize2 + (m.f * resources.getDimensionPixelSize(c.b.a.b.d.mtrl_calendar_day_height)) + ((m.f - 1) * resources.getDimensionPixelOffset(c.b.a.b.d.mtrl_calendar_month_vertical_padding)) + resources.getDimensionPixelOffset(c.b.a.b.d.mtrl_calendar_bottom_padding);
    }

    private static int w(Context context) {
        Resources resources = context.getResources();
        int dimensionPixelOffset = resources.getDimensionPixelOffset(c.b.a.b.d.mtrl_calendar_content_padding);
        int i = l.s().f;
        return (dimensionPixelOffset * 2) + (resources.getDimensionPixelSize(c.b.a.b.d.mtrl_calendar_day_width) * i) + ((i - 1) * resources.getDimensionPixelOffset(c.b.a.b.d.mtrl_calendar_month_horizontal_padding));
    }

    private int y(Context context) {
        int i = this.t;
        return i != 0 ? i : this.u.c(context);
    }

    private void z(Context context) {
        this.C.setTag(H);
        this.C.setImageDrawable(t(context));
        s.e0(this.C, (b.f.l.a) null);
        E(this.C);
        this.C.setOnClickListener(new d());
    }

    public final Dialog h(Bundle bundle) {
        Dialog dialog = new Dialog(requireContext(), y(requireContext()));
        Context context = dialog.getContext();
        this.A = A(context);
        int c2 = c.b.a.b.z.b.c(context, c.b.a.b.b.colorSurface, i.class.getCanonicalName());
        g gVar = new g(context, (AttributeSet) null, c.b.a.b.b.materialCalendarStyle, k.Widget_MaterialComponents_MaterialCalendar);
        this.D = gVar;
        gVar.L(context);
        this.D.U(ColorStateList.valueOf(c2));
        this.D.T(s.s(dialog.getWindow().getDecorView()));
        return dialog;
    }

    public final void onCancel(DialogInterface dialogInterface) {
        Iterator it = this.r.iterator();
        while (it.hasNext()) {
            ((DialogInterface.OnCancelListener) it.next()).onCancel(dialogInterface);
        }
        super.onCancel(dialogInterface);
    }

    public final void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        if (bundle == null) {
            bundle = getArguments();
        }
        this.t = bundle.getInt("OVERRIDE_THEME_RES_ID");
        this.u = (d) bundle.getParcelable("DATE_SELECTOR_KEY");
        this.w = (a) bundle.getParcelable("CALENDAR_CONSTRAINTS_KEY");
        this.y = bundle.getInt("TITLE_TEXT_RES_ID_KEY");
        this.z = bundle.getCharSequence("TITLE_TEXT_KEY");
    }

    public final View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
        View inflate = layoutInflater.inflate(this.A ? h.mtrl_picker_fullscreen : h.mtrl_picker_dialog, viewGroup);
        Context context = inflate.getContext();
        if (this.A) {
            inflate.findViewById(f.mtrl_calendar_frame).setLayoutParams(new LinearLayout.LayoutParams(w(context), -2));
        } else {
            View findViewById = inflate.findViewById(f.mtrl_calendar_main_pane);
            View findViewById2 = inflate.findViewById(f.mtrl_calendar_frame);
            findViewById.setLayoutParams(new LinearLayout.LayoutParams(w(context), -1));
            findViewById2.setMinimumHeight(u(requireContext()));
        }
        TextView textView = (TextView) inflate.findViewById(f.mtrl_picker_header_selection_text);
        this.B = textView;
        s.g0(textView, 1);
        this.C = (CheckableImageButton) inflate.findViewById(f.mtrl_picker_header_toggle);
        TextView textView2 = (TextView) inflate.findViewById(f.mtrl_picker_title_text);
        CharSequence charSequence = this.z;
        if (charSequence != null) {
            textView2.setText(charSequence);
        } else {
            textView2.setText(this.y);
        }
        z(context);
        this.E = (Button) inflate.findViewById(f.confirm_button);
        if (this.u.f()) {
            this.E.setEnabled(true);
        } else {
            this.E.setEnabled(false);
        }
        this.E.setTag(F);
        this.E.setOnClickListener(new a());
        Button button = (Button) inflate.findViewById(f.cancel_button);
        button.setTag(G);
        button.setOnClickListener(new b());
        return inflate;
    }

    public final void onDismiss(DialogInterface dialogInterface) {
        Iterator it = this.s.iterator();
        while (it.hasNext()) {
            ((DialogInterface.OnDismissListener) it.next()).onDismiss(dialogInterface);
        }
        ViewGroup viewGroup = (ViewGroup) getView();
        if (viewGroup != null) {
            viewGroup.removeAllViews();
        }
        super.onDismiss(dialogInterface);
    }

    public final void onSaveInstanceState(Bundle bundle) {
        super.onSaveInstanceState(bundle);
        bundle.putInt("OVERRIDE_THEME_RES_ID", this.t);
        bundle.putParcelable("DATE_SELECTOR_KEY", this.u);
        a.b bVar = new a.b(this.w);
        if (this.x.o() != null) {
            bVar.b(this.x.o().h);
        }
        bundle.putParcelable("CALENDAR_CONSTRAINTS_KEY", bVar.a());
        bundle.putInt("TITLE_TEXT_RES_ID_KEY", this.y);
        bundle.putCharSequence("TITLE_TEXT_KEY", this.z);
    }

    public void onStart() {
        super.onStart();
        Window window = i().getWindow();
        if (this.A) {
            window.setLayout(-1, -1);
            window.setBackgroundDrawable(this.D);
        } else {
            window.setLayout(-2, -2);
            int dimensionPixelOffset = getResources().getDimensionPixelOffset(c.b.a.b.d.mtrl_calendar_dialog_background_inset);
            Rect rect = new Rect(dimensionPixelOffset, dimensionPixelOffset, dimensionPixelOffset, dimensionPixelOffset);
            window.setBackgroundDrawable(new InsetDrawable(this.D, dimensionPixelOffset, dimensionPixelOffset, dimensionPixelOffset, dimensionPixelOffset));
            window.getDecorView().setOnTouchListener(new c.b.a.b.t.a(i(), rect));
        }
        B();
    }

    public void onStop() {
        this.v.c();
        super.onStop();
    }

    public String v() {
        return this.u.b(getContext());
    }

    public final S x() {
        return this.u.a();
    }
}
